package com.quanyue.music.file.mapper.provider;

import com.quanyue.auto.util.StringUtils;
import com.quanyue.music.file.type.FMDetailRequest;
import com.quanyue.music.file.type.FMListExcel;
import org.apache.ibatis.jdbc.SQL;

public class FMFileListProvider {
    public String findExcelList(FMListExcel request) {
        SQL sql = new SQL();
        sql.SELECT("f.*, t.tag as tag_name")
                .FROM("qy_fm f")
                .LEFT_OUTER_JOIN("qy_music_tag t on f.tag = t.id")
                .WHERE("f.del_flag = false");
        if (request.getFmType() != null) {
            sql.AND().WHERE("f.type = '" + request.getFmType() + "'");
        }
        if (request.getStatus() != null) {
            sql.AND().WHERE("f.status = '" + request.getStatus() + "'");
        }
        if (StringUtils.isNotBlank(request.getFmTitle())) {
            sql.AND().WHERE("f.title like '%" + request.getFmTitle() + "%'");
        }
        if (StringUtils.isNotBlank(request.getTag())) {
            sql.AND().WHERE("f.tag = '" + request.getTag() + "'");
        }
        if (request.getStartDateTime() != null && request.getEndDateTime() != null) {
            sql.AND().WHERE("f.create_time > '" + request.getStartDateTime() + "'")
                    .AND().WHERE("f.create_time < '" + request.getEndDateTime() + "'");
        }
        if (StringUtils.isNotBlank(request.getKeyId())) {
            sql.AND().WHERE("f.key_id = '" + request.getKeyId() + "'");
        }
        sql.ORDER_BY("f.key_id");
        return sql.toString();
    }

    public String findList(FMDetailRequest request) {
        SQL sql = new SQL();
        sql.SELECT("f.*, t.tag as tag_name")
                .FROM("qy_fm f")
                .LEFT_OUTER_JOIN("qy_music_tag t on f.tag = t.id")
                .WHERE("f.del_flag = false");
        if (request.getFmType() != null) {
            sql.AND().WHERE("f.type = '" + request.getFmType() + "'");
        }
        if (request.getStatus() != null) {
            sql.AND().WHERE("f.status = '" + request.getStatus() + "'");
        }
        if (StringUtils.isNotBlank(request.getFmTitle())) {
            sql.AND().WHERE("f.title like '%" + request.getFmTitle() + "%'");
        }
        if (StringUtils.isNotBlank(request.getTag())) {
            sql.AND().WHERE("f.tag = '" + request.getTag() + "'");
        }
        if (request.getStartDateTime() != null && request.getEndDateTime() != null) {
            sql.AND().WHERE("f.create_time > '" + request.getStartDateTime() + "'")
                    .AND().WHERE("f.create_time < '" + request.getEndDateTime() + "'");
        }
        if (StringUtils.isNotBlank(request.getKeyId())) {
            sql.AND().WHERE("f.key_id = '" + request.getKeyId() + "'");
        }
        return sql.toString();
    }
}
